package com.xgx.faceloginback.helloworld;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTCreationException;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import io.jboot.Jboot;
import io.jboot.utils.StringUtils;
import io.jboot.web.controller.JbootController;
import io.jboot.web.controller.annotation.RequestMapping;
import io.jboot.web.cors.EnableCORS;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Michael Yang 杨福海 （fuhai999@gmail.com）
 * @version V1.0
 * @Package io.jboot.demo.helloworld
 */
@EnableCORS
@RequestMapping("/")
public class Helloworld extends JbootController {


    public static void main(String[] args) {
        Jboot.run(args);
    }


    public void index() {
        String token="";
        try {
            Algorithm algorithm = Algorithm.HMAC256("secret");
            token= JWT.create()
                    .withIssuer("auth0")
                    .sign(algorithm);
        } catch (UnsupportedEncodingException exception){
            //UTF-8 encoding not supported
        } catch (JWTCreationException exception){
            //Invalid Signing configuration / Couldn't convert Claims.
        }
        renderText(token);
    }
    public void show(){

        renderText(getJwtPara("terrydash"));
    }
    public void ver(String token)
    {
        System.out.println("token:"+token);
        System.out.println(getPara("token"));
        if (StringUtils.isBlank(token))
        {
            renderText("token为空！");
            return;
        }


        DecodedJWT jwt=null;
        try {
            Algorithm algorithm = Algorithm.HMAC256("secret");
            JWTVerifier verifier = JWT.require(algorithm)
                    .withIssuer("auth0")
                    .build(); //Reusable verifier instance
            jwt= verifier.verify(token);
        } catch (UnsupportedEncodingException exception){
            //UTF-8 encoding not supported
        } catch (JWTVerificationException exception){
            //Invalid signature/claims
            renderText("验证不正确!");
        }
        System.out.println(jwt.getSignature());
        System.out.println(jwt.getPayload());
        renderText("验证正确");
    }
    public void dbshow(){
        Page<Record> userPage = Db.paginate(1, 10, "select * ","from department");
        renderJson(userPage.getList());
    }

}
